package controller

import (
	"fx_swift_ifcgate/api"
	"fx_swift_ifcgate/internal/service"
	"fx_swift_ifcgate/pkg/code"
	"fx_swift_ifcgate/pkg/internalerrors"
	"fx_swift_ifcgate/pkg/validator"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InternationTransfer(c *gin.Context) {
	req := &api.InternationTransferReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	err := service.NewTransferSrv().InternationTransfer(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func DomesticTransfer(c *gin.Context) {
	req := &api.DomesticTransferReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}
	err := service.NewTransferSrv().DomesticTransfer(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func InternationTransferList(c *gin.Context) {
	req := &api.ITListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewTransferSrv().InternationTransferList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func DomesticTransferList(c *gin.Context) {
	req := &api.DTListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewTransferSrv().DomesticTransferList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func DomesticTransferDetail(c *gin.Context) {
	merOrderNo := c.Query("merOrderNo")
	result, err := service.NewTransferSrv().DomesticTransferDetail(c, merOrderNo)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func TradeReceiptList(c *gin.Context) {
	req := &api.TradeReceiptListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewTransferSrv().TradeReceiptList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func SubmitTradeOrder(c *gin.Context) {
	req := &api.SubmitTradeOrderReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewTransferSrv().SubmitTradeOrder(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func TradeOrderMappingInfo(c *gin.Context) {
	flowNo := c.Query("flowNo")
	result, err := service.NewTransferSrv().TradeOrderMappingInfo(c, flowNo)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func TradeOrderDetail(c *gin.Context) {
	flowNo := c.Query("flowNo")
	result, err := service.NewTransferSrv().TradeOrderDetail(c, flowNo)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func CheckoutTradeAmount(c *gin.Context) {
	req := &api.CheckoutTradeAmountReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewTransferSrv().CheckoutTradeAmount(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
